package com.ostrich.cloud.ostrichUserService.app.controller;



import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ostrich.cloud.common.result.PageResult;
import com.ostrich.cloud.common.result.Result;
import com.ostrich.cloud.ostrichUserService.app.config.JwtHelper;
import com.ostrich.cloud.ostrichUserService.app.utils.GetParentId;

import com.ostrich.cloud.ostrichUserService.client.RiderMangerClient;
import com.ostrich.cloud.ostrichUserService.infra.model.Rider;
import com.ostrich.cloud.ostrichUserService.infra.model.UserParent;

import com.ostrich.cloud.ostrichUserService.service.RiderService;
import com.ostrich.ostrichUserService.dto.RiderDto;
import com.ostrich.ostrichUserService.param.LoginCusParam;
import com.ostrich.ostrichUserService.param.rider.RegRider;
import com.ostrich.ostrichUserService.param.rider.UpdateRider;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 * 骑手表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2022-10-26
 */
@RestController
public class RiderController implements RiderMangerClient {




    @Resource
    private RiderService riderService;

    @Resource
    private JwtHelper jwtHelper;

    @Override
    public Result riderRegister(RegRider regRider) {

        riderService.riderRegister(regRider);
        return Result.ok();
    }

    @Override
    public Result updateRiderState(Integer riderId, String status) {
        riderService.updateRiderState(riderId,status);
        return null;
    }

    @Override
    public Result riderLogin(LoginCusParam loginCusParam) {
        UserParent userParent = riderService.businessmanLogin(loginCusParam);
        Date date = DateUtils.addDays(new Date(), 1);
        HashMap<String, Object> map = new HashMap<>();
        map.put("operatorId",userParent.getParentId());
        map.put("operatorName","rider");
        String jwt = jwtHelper.createJWT(date, map);
        //  第一步：比对前端提交的账号名称、密码
        return Result.ok(jwt);
    }

    @Override
    public Result getRiderById() {
        Integer parentId = GetParentId.getParentId();
        Rider rider = riderService.getOne(new QueryWrapper<Rider>().eq("parent_id", parentId));
        RiderDto riderDto = BeanUtil.copyProperties(rider, RiderDto.class);
        return Result.ok(riderDto);
    }

    @Override
    public Result getAllRider(Integer current, Integer size, String status,Integer businessmanId) {
        PageResult<RiderDto> sAllRider = riderService.getSAllRider(current, size, status,businessmanId);

        return Result.ok(sAllRider);
    }

    @Override
    public Result getRider(Integer riderId) {
        Rider byId = riderService.getById(riderId);
        RiderDto riderDto = BeanUtil.copyProperties(byId, RiderDto.class);
        return Result.ok(riderDto);
    }

    @Override
    public Result updateRider(UpdateRider updateRider) {

        Integer parentId = GetParentId.getParentId();
        Rider rider = BeanUtil.copyProperties(updateRider, Rider.class);
        rider.setParentId(parentId);
        riderService.updateRider(rider);
        return Result.ok();
    }

    @Override
    public Result addMoney(Integer riderId, Double money) {
        riderService.addMoney(riderId,money);
        return Result.ok();
    }

    @Override
    public Result getCustomerLocByRider(String latitude, String logitud, Integer customerLocationId) {

        Double dist = riderService.getCustomerLocByRider(latitude, logitud, customerLocationId);
        return Result.ok(dist);
    }

}

